package com.iot.tenant.controller;

import com.dc3.common.bean.R;
import com.iot.tenant.pojo.Dc3Tenant;
import com.iot.tenant.pojo.Dc3TenantBind;
import com.iot.tenant.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @Author 咕唧
 * @Date 2022/7/14 20:35
 * @Version 1.0
 */
@RestController
@RequestMapping("/tenant")
public class TenantController {
    private TenantService tenantService;

    @Autowired
    public void setTenantService(TenantService tenantService) {
        this.tenantService = tenantService;
    }

    @PostMapping("/add/dc3")
    public Mono<R<Object>> TenantService(@RequestBody Mono<Dc3Tenant> dc3Tenant) {
        return tenantService.addTenant(dc3Tenant).map(R::ok);
    }

    @GetMapping("/info/{id}")
    public Mono<R<Dc3Tenant>> findById(@PathVariable String id) {
        return tenantService.findById(id).map(R::ok).defaultIfEmpty(R.fail("数据不存在"));
    }

    @GetMapping("/list/{name}")
    public Mono<R<List<Dc3Tenant>>> findByName(@PathVariable String name) {
        return tenantService.findByName(name).collectList().map(R::ok).defaultIfEmpty(R.fail("数据不存在"));
    }

    @PostMapping("/update")
    Mono<R<Object>> updateTenant(@RequestBody Mono<Dc3Tenant> tenantMono) {
        return tenantService.updateTenant(tenantMono);
    }

    @GetMapping("/info/and/user/{tenantId}")
    Mono<R> findWithUsers(@PathVariable String tenantId) {
        return tenantService.findWithUsers(tenantId).map(R::ok);
    }

    @PostMapping("/usercounts")
    public Mono<R> getCountByTentIdAndUserIds(@RequestBody List<String> userIds, String tenantId) {
        return tenantService.findCountByTenantIdAndUserId(tenantId, userIds).map(R::ok);
    }

    @PostMapping("/binduser")
    public Mono<R<Object>> bindUser2Tenant(@RequestBody Mono<Dc3TenantBind> dc3TenantBind){
        return tenantService.bindUser2Tenant(dc3TenantBind);
    }
}
